package com.example.demo.spider.pipeline;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.demo.entity.News;
import com.example.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Component
public class DbPipeline implements Pipeline {
    @Autowired
    private NewsService newsService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Object v : resultItems.getAll().values()) {
            List<News> list = new ArrayList<>();
            if (v instanceof News) list.add((News) v);
            else list.addAll((List<News>) v);
            newsService.saveOrUpdateBatch(list);
            list.forEach(this::download);
        }
    }

    @Async
    private void download(News news) {
        // 下载标题图片
        String base_path = "data/" + LocalDate.now().plusDays(-1);
        if (StrUtil.isNotBlank(news.getImg())) {
            String name = FileUtil.getName(news.getImg());
            HttpUtil.downloadFile(news.getImg(), base_path + "/img/" + name);
            return;
        }
        // 下载正文图片
        if (CollUtil.isNotEmpty(news.getImages())) {
            for (String src : news.getImages()) {
                String name = FileUtil.getName(src);
                HttpUtil.downloadFile(src, base_path + "/images/" + name);
            }
        }
        // 下载音频视频
        if (StrUtil.isNotBlank(news.getVideo())) {
            List<String> list = JSON.parseArray(news.getVideo(), String.class);
            for (String src : list) {
                String name = FileUtil.getName(src);
                HttpUtil.downloadFile(src, base_path + "/videos/" + name);
            }
        }
    }
}
